package com.idroid.activities;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MapActivity extends Activity {

	MapView mapView;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		mapView = new MapView(this, 256);
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(true);
		mapView.setDrawingCacheEnabled(true);
//		mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);

		mapView.setTileSource(TileSourceFactory.MAPNIK);

		mapView.getController().setZoom(15);
		mapView.getController().setCenter(new GeoPoint(44.97699, -93.231353));

		Overlay overlay = new Overlay(this) {

			@Override
			protected void draw(Canvas arg0, MapView arg1, boolean arg2) {

			}

			@Override
			public boolean onTouchEvent(MotionEvent event, MapView mapView) {
				// ---when user lifts his finger---
				if (event.getAction() == MotionEvent.ACTION_UP) {
					Point touchedPoint = new Point((int) event.getX(),
							(int) event.getY());

					IGeoPoint pt = mapView.getProjection().fromPixels(
							touchedPoint.x, touchedPoint.y);
					Toast.makeText(
							MapActivity.this,
							"You touched here: "
									+ ((double) pt.getLatitudeE6() / 1E6) + " "
									+ ((double) pt.getLongitudeE6() / 1E6),
							Toast.LENGTH_LONG).show();

				}

				return false;
			}

		};

		super.setTitle("MapActivity");
		mapView.getOverlays().add(overlay);
		mapView.invalidate();

		setContentView(mapView);

	}

}